class Solution {
public:
    int numTrees(int n) {
        vector<int> f(2,1);
        for (int i=2;i<=n;i++) {
            int sum=0;
            for (int j=0;j<=i-1;j++)
                sum+=f[j]*f[i-1-j];
            f.push_back(sum);
        }
        return f[n];
    }
};
